// app.js

App({
  globalData: {
    // request_url: 'https://visitor.zuru.cloud:5014/',
     request_url: 'http://10.26.14.147:8064/',
  },

  setLoginData(e){
    e.setData({login: this.globalData.user_status})
    e.setData({user_name: this.globalData.user_name})
  },

  odoo_login(code){
    console.log("odoo login")
    console.log(code)
    let that = this
 
    wx.request({
      url: that.globalData.request_url + "factory/user_login",
      method:"POST",
      data:{"code":code},
      dataType:"json",
      success (resp) {
        console.log(resp)
        console.log(resp.data.result.user_name)
        if(resp.data.result.code==200){
          console.log('code')
          console.log(resp.data.result.code)
          // wx.setStorage(
          //   {
          //     key:"cookies",
          //     data:resp.cookies[0]
          //   })
            console.log("已登陆")
            that.globalData.cookies = resp.cookies[0]
            console.log('cookie是' + resp.cookies[0])
            that.globalData.user_status = "has_login"
            that.globalData.user_name = resp.data.result.user_name
            that.globalData.choose_start_time = resp.data.result.choose_start_time
        }else if(resp.data.result.code==403){
          console.log('未登录')
          that.globalData.user_status = "no_login"
          that.globalData.user_name = resp.data.result.user_name
          that.globalData.cookies = resp.cookies[0]
          that.globalData.choose_start_time = resp.data.result.choose_start_time
        }
      },
      fail (err){
        console.log('失败')
        console.log(err)
      }
    })
  },
  
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    let that = this
    // 登录
    wx.login({
      success (res) {
        that.globalData.login_res = res
        if (res.code) {
          that.odoo_login(res.code)
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })

  },
})
